package Application.Controller;

import Application.client.OtherServiceClient;
import Application.client.SearchClient;
import Application.entity.Customer;
import Application.service.CustomerService;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CustomerController {
    @Value("${version}")
    private String version;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private SearchClient searchClient;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private OtherServiceClient otherServiceClient;
    @GetMapping("list")
    public String list(){
        return otherServiceClient.list();
    }
    @GetMapping("/version")
    public String version() throws InterruptedException {
        Thread.sleep(3000);
        return version;
    }
    @GetMapping("/customer")
    public String customer() {
//        System.out.println("coustomer");
//        //1 通过eurekaClient获取search服务的信息
//        InstanceInfo info = eurekaClient.getNextServerFromEureka("SEARCH", false);
//        //2 获取到访问的地址
//        String homePageUrl = info.getHomePageUrl();
//        System.out.println(homePageUrl);
//        //3 通过restTemplate访问
//        String result = restTemplate.getForObject(homePageUrl + "/search", String.class);
        //robbin使用访问
//        String result= restTemplate.getForObject("http://SEARCH/search", String.class);
        //feign使用
        String result= searchClient.search();
        return "测试："+result;
    }

    @GetMapping(value = "/customer/{id}")
    @HystrixCommand(fallbackMethod = "searchByIdFactory",commandProperties ={
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name ="circuitBreaker.enabled" ,value = "true"),
            @HystrixProperty(name ="circuitBreaker.requestVolumeThreshold" ,value = "10"),
            @HystrixProperty(name ="circuitBreaker.sleepWindowInMilliseconds" ,value = "5000"),
            @HystrixProperty(name ="circuitBreaker.errorThresholdPercentage" ,value = "70")

    } )
    public Customer searchById(@PathVariable(value = "id") Integer id) {
        System.out.println(customerService.searchById(id));
        System.out.println(customerService.searchById(id));
        customerService.clearCache(id);
        System.out.println(customerService.searchById(id));
        System.out.println(customerService.searchById(id));
        return searchClient.searchById(id);
    }
//searchById的降级方法 方法的描述要和接口一致
    public Customer searchByIdFactory(@PathVariable(value = "id") Integer id){
        return new Customer("xxxx", -1, -1);
    }
    @GetMapping(value = "/getCustomer")
    public  Customer getCustomer(@RequestParam(value = "id") Integer id,@RequestParam(value = "name") String name){
        return searchClient.getCustomer(id, name);
    }
    @GetMapping(value = "/save")
    public  Customer save(Customer customer){
        return searchClient.save(customer);
    }
}
